/*
 * CarManagerDatabase.java
 *
 * Car Manager
 *
 * Copyright (C) 2005 Tommi Laukkanen
 * http://www.substanceofcode.com
 *
 * Created on June 6th 2005, 19:52
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
package com.substanceofcode.carmanager.model;

import javax.microedition.rms.*;

/**
 * This class is an implementation for the Database class specific to
 * Fill-up records.
 */
public class CarManagerDatabase extends Database {
    
    /** Default Constructor */
    public CarManagerDatabase() {
        rc = new SubComparator();
    }
    
    /** Record enumeration */
    public synchronized RecordEnumeration enumerateRecords()
    throws RecordStoreNotOpenException {
        return database.enumerateRecords(new SubFilter(), null, false);
    }
    
    /** Filter's the last record ID */
    private class SubFilter implements RecordFilter {
        
        /** Returns true if the candidate is less than 5 characters */
        public boolean matches(byte[] candidate) {
            return ((candidate.length > 5) ? true : false);
        }
        
    }
    
    /** Compare two records */
    private class SubComparator implements RecordComparator {
        
        public int compare(byte[] rec1, byte[] rec2) {
            try {
                String firstEntryRecord = new String(rec1);
                String secondEntryRecord = new String(rec2);
                
                if(rec1.length <= 5) return RecordComparator.PRECEDES;
                if(rec2.length <= 5) return RecordComparator.PRECEDES;
                
                FillupEntry tank1 = FillupEntryParser.parseEntry(firstEntryRecord);
                FillupEntry tank2 = FillupEntryParser.parseEntry(secondEntryRecord);
                long trip1 = tank1.getKm();
                long trip2 = tank2.getKm();
                long intResult = trip2 - trip1;
                
                if ( intResult < 0 ) {
                    return RecordComparator.PRECEDES;
                } else if ( intResult == 0) {
                    return RecordComparator.EQUIVALENT;
                } else {
                    return RecordComparator.FOLLOWS;
                }
            } catch(Exception e) {
                System.out.println("Compare ERROR:" + e.getMessage());
                return RecordComparator.EQUIVALENT;
            }
        }
    }
}

